package code;

import java.io.*;
import java.util.LinkedList;

import javax.activity.InvalidActivityException;
import javax.swing.JFrame;

public class BoardBuilder
{	
	static public Board loadBoard(String filename) throws IOException, InvalidFileException
	{
		char[][] info = readFile(filename);
		int rows = info.length, columns = info[0].length;
		boolean hasSwitch = false, hasGoal = false, hasPlayer = false;
			
		Player levelPlayer = new Player("P");
		Board board = new Board(rows, columns, levelPlayer);
		GoalCell levelGoal = new GoalCell("_", levelPlayer);
		
		
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
			{
				switch (info[i][j])
				{
					case '@':
						if (!hasPlayer)
						{
							Ground aux = new Ground("_");
							aux.placeObject(levelPlayer);
							board.setCell(i, j, aux);
							hasPlayer = true;
						}
						else
							throw new InvalidFileException("El tablero debe tener un jugador.");
					break;
					
					case 'G':
						if (!hasGoal)
						{
							board.setCell(i, j, levelGoal);
							hasGoal = true;
						}
						else 
							throw new InvalidFileException("El tablero solo puede tener un destino.");
					break;
						
					case '#':
						WaterCell water = new WaterCell("~");
						board.setCell(i, j, water);
						
					break;
						
					case 'B':
						Crate crate = new Crate("C");
						Ground ground = new Ground("_");
						ground.placeObject(crate);
						board.setCell(i, j, ground);					
					break;
						
					case 'C':
						IceCube ice = new IceCube("H");
						Ground floor = new Ground("_");
						floor.placeObject(ice);
						board.setCell(i, j, floor);
					break;
						
					case 'K':
						if (!hasSwitch)
						{
							SwitchCell levelSwitch = new SwitchCell("#", levelGoal);
							board.setCell(i, j, levelSwitch);
							hasSwitch = true;
						}
						else 
							throw new InvalidFileException("El tablero puede tener como maximo un interruptor.");
					break;
						
					case 'T':
						TreeCell tree = new TreeCell("&");
						board.setCell(i, j, tree);
					break;
					
					case ' ':
						Ground grnd = new Ground("_");
						board.setCell(i, j, grnd);
					break;
						
					default:
						throw new InvalidFileException("Caracter invalido encontrado.");
				}
			}
		}
		
		if (!hasSwitch)
			levelGoal.setVisible(true);
		
		if (!hasGoal || !hasPlayer)
			throw new InvalidFileException("El mapa es invalido.");
		
		board.prepareTiles();
		
		return board;
	}
	
	static private char[][] readFile(String filename) throws IOException, InvalidFileException 
	{
		char[][] data;
		FileReader fileinput;
		BufferedReader reader;

		fileinput = new FileReader(filename);			
		reader = new BufferedReader(fileinput);
			
		data = interpretFile(reader);
		
		return data;
	}
	
	static private char[][] interpretFile(BufferedReader reader) throws IOException, InvalidFileException
	{
		String aux;
		int linelength;
		int lines;
		
		LinkedList<String> data = new LinkedList<String>();
		char[][] matrix;
		
		
		while ((aux = reader.readLine()) != null)
			data.add(aux);		 
		
	
		linelength = data.getFirst().length();
		lines = data.size();
		
		if (lines == 0)
			return null;
		
		matrix = new char[lines][linelength];
		
		int i = 0;
		
		for (String line : data)
		{
			if (line.length() != linelength)
				throw new InvalidFileException("Archivo invalido.");
			matrix[i++] = line.toCharArray();
		}
		
		return matrix;
	}
	
}
